﻿using System;
using System.Data;
using Guru.Data;
using HSM.Data;
using HSM.Entities;

namespace HSM.Business
{
    public partial class HSMBusiness : HSMData
    {
        GuruParameterCollection parameters = new GuruParameterCollection();
        public UserLogin GetUserDetail(Guid userID)
        {
            parameters.Clear();
            parameters.Add("@UserID", userID, DbType.Guid);
            DataTable userInfo = GetDataTable("spa_SEC_Users_Get", parameters);
            if (userInfo.Rows.Count > 0)
            {
                return UserLogin.CreateEntity(userInfo.Rows[0]);
            }
            return null;
        }

        public UserLogin GetUserDetail(int clientID, int hotelID, string username)
        {
            parameters.Clear();
            parameters.Add("@ClientID", clientID, DbType.Int32);
            parameters.Add("@HotelID", hotelID, DbType.Int32);
            parameters.Add("@UserName", username, DbType.String);
            DataTable userInfo = GetDataTable("spa_SEC_Users_Get", parameters);
            if (userInfo.Rows.Count > 0)
            {
                return UserLogin.CreateEntity(userInfo.Rows[0]);
            }
            return null;
        }

        public void UpdateUser(byte mode, UserLogin user)
        {
            UpdateData("spa_SEC_UpdateUserInfo", mode, user.UserID, user.UserNameFirst, user.UserNameLast, user.UserPassword, user.UserPasswordType, user.Status, user.Email);
        }

        public Guid CreateUser(UserLogin user, int roleID, Guid createdBy)
        {
            UpdateData("spa_SEC_Users_Insert", user.UserID, user.UserName, user.ClientID, user.Email, user.UserNameFirst, user.UserNameLast, null, user.UserPassword, user.UserPasswordType, user.Status, roleID, createdBy);
            return new Guid(GetParamOut("@UserID").ToString());
        }

        public DataTable GetRoles(int roleID)
        {
            GuruParameterCollection parameters = new GuruParameterCollection();
            if (roleID > 0)
                parameters.Add("@RoleID", roleID, DbType.Int32);
            DataTable roleList = GetDataTable("spa_SEC_GetRoles", parameters);
            return roleList;
        }

        public int ValidateLogin(int clientID, int hotelID, string username, string password, string remoteIP)
        {
            DataTable user = GetDataTable("spa_SEC_Login", clientID, hotelID, username, password, remoteIP, "", "");
            int ret = (int)user.Rows[0][0] << 24;
            ret = ret | (int)user.Rows[0][1];
            return ret;
        }
        public DataTable GetUserRoles(Guid userID)
        {
            return GetDataTable("spa_SEC_Roles_GetByUser", userID);
        }
        public DataTable GetUserPermissions(Guid userID)
        {
            return GetDataTable("spa_SEC_Users_Permissions_Get", userID);
        }

        public DataTable GetUserMenus(Guid userID)
        {
            return GetDataTable("spa_SEC_GetMenuItemForUser", userID);
        }

        public void AssignPermissionToRole(string permissionCode, int roleID, bool isDeleteAll)
        {
            parameters.Add("@PermissionCode", permissionCode, DbType.String);
            parameters.Add("@RoleID", roleID, DbType.Int32);
            parameters.Add("@IsDeleteAll", isDeleteAll, DbType.Boolean);
            UpdateData("spa_SEC_AssignPermissionToRole", parameters);
        }
    }
}
